var config = $.extend(true, {}, listApp, {
    data: {
        service: protocolService,
        searchVm: {
            name: '',
            net_id: '',
            stype: ''
        },
        mrouter: {
            pagePath: top._CTX + '/web/protocol/main',
        },
        contentHeight: 0,
        options: [{
            label: '捷宸',
            value: '捷宸'
        }, {
            label: 'SNMP',
            value: 'snmp'
        }, {
            label: 'RePing',
            value: 'RePing'
        }],
        snmp_versions: [{
            label: 'v1',
            value: 1
        }, {
            label: 'v2',
            value: 2
        }, {
            label: 'v3',
            value: 3
        }],
        requestPagePath: 'requestPageExt',
        nets: [],
        tabledatas: [],
        editVm: {
            addTitle: '添加',
            inputTitle: '导入',
            editFormRules: {
                formRules: {
                    stype: [{required: true, message: '请选择协议类型', trigger: 'change'}],
                    name: [{required: true, message: '请输入名称', trigger: 'change'}],
                    netId: [{required: true, message: '请选择网关', trigger: 'change'}],
                    snmpv: [{required: true, message: '请选择协议版本', trigger: 'change'}]
                }
            }
        },
        fileList: [],
        popoverVisible: false,
        currentIndex: -1,
        selectorLoading: false
    },
    methods: {
        init: function () {
            this.getNets()
        },
        initHeight: function () {
            let that = this;
            setTimeout(() => {
                let height = document.documentElement.clientHeight;
                that.contentHeight = height - 30
                // console.log("height", that.contentHeight)
            }, 200)
        },
        mdeleteRow: function (row) {
            var _this = this;
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                _this.deleteLoading = true;
                _HTTP.delete("/api/protocol/deleteProtocol/" + row.id, null, {
                    success: function (ret) {
                        _this.deleteLoading = false;
                        if (ret.content === '删除成功！') {
                            _this.showSuccessMsg("操作成功！");
                        } else {
                            _this.showFailMsg(ret.content)
                        }
                        _this.buildCurrentSearch();
                        if (_this.currentSearch.pageInfo.pageIndex > 1)
                            _this.currentSearch.pageInfo.pageIndex--;
                        _this.requsetPage();
                    },
                    fail: function (res) {
                        _this.deleteLoading = false;
                        _this.showFailMsg(res.msg);
                    }
                })
            });
        },
        /**
         * 导入
         * @param data
         */
        uploadHttpRequest: function (data) {
            // console.log(data.file)
            if (data.file.type === 'text/xml') {
                this.inputXml(data)
            }
            if (data.file.type === 'application/vnd.ms-excel') {
                this.inputCsv(data)
            }
        },
        /**
         * 导入csv
         * @param data
         */
        inputCsv(data) {
            let _this = this
            let fileFormData = new FormData();
            fileFormData.append('file', data.file, data.file.name);
            let requestConfig = {
                headers: {
                    'Content-Type': 'multipart/form-data'
                },
            }
            this.$http.post("/api/protocol/uploadCsv", fileFormData, requestConfig).then((res) => {
                if (res.data.code === 200) {
                    if (res.data.data.content === '导入成功！') {
                        _this.showSuccessMsg(res.data.data.content)
                        _this.requsetPage()
                    } else {
                        _this.showFailMsg(res.data.data.content)
                        _this.requsetPage()
                    }
                } else {
                    console.log(res)
                    _this.requsetPage()
                }
            })
        },
        /**
         * 导入xml
         * @param data
         */
        inputXml(data) {
            let reader = new FileReader();
            let _this = this;
            const req = {
                content: ''
            }
            reader.readAsText(data.file)
            reader.onload = function () {
                // console.log(this.result)
                let that = _this
                req.content = this.result
                _HTTP.post("/api/protocol/uploadProtocols", req, {
                    success(res) {
                        // console.log(res.content)
                        that.requsetPage()
                        if (res.content === '导入成功！') {
                            that.showSuccessMsg(res.content)
                        } else {
                            that.$message({
                                message: res.content,
                                type: 'fail',
                                showClose: 'true',
                                duration: 0
                            });
                        }
                        that.clearFiles()
                    },
                    fail(res) {
                        console.log(res)
                        that.clearFiles()
                        that.requsetPage()
                    }
                })
            }
        },
        /**
         * 导出Excel（.csv格式）
         * @param protocolName
         * @param protocolId
         */
        excelOutput: function (protocolName, protocolId) {
            this.popoverVisible = false;
            let _this = this;
            this.tableVm.loading = true;
            _HTTP.get("/api/protocol/getProtocolOutCsv?protocolId=" + protocolId, null, {
                success(res) {
                    console.log(res.content)
                    if (res.content !== '导出失败！') {
                        _this.tableVm.loading = false;
                        // console.log(res.content)
                        // const blob = new Blob([res.content], {
                        //     type:
                        //         "application/vnd.ms-excel;charset=utf-8"
                        // });
                        // if (window.navigator.msSaveBlob) {
                        //     //没有此判断的话，ie11下的导出没有效果
                        //     window.navigator.msSaveBlob(blob, unescape(protocolName + ".csv"));
                        // } else {
                            const downloadElement = document.createElement("a");
                            // const href = window.URL.createObjectURL(blob); //创建下载的链接
                            downloadElement.href = res.content;
                            downloadElement.download = unescape(protocolName + ".csv");
                            document.body.appendChild(downloadElement);
                            downloadElement.click(); //点击下载
                            document.body.removeChild(downloadElement); //下载完成移除元素
                            // window.URL.revokeObjectURL(href); //释放掉blob对象
                        // }
                    } else {
                        _this.showFailMsg(res.content)
                    }
                },
                fail(res) {
                    console.log(res)
                }
            })
        },
        /**
         * 导出
         * @param protocolName
         * @param protocolId
         */
        output: function (protocolName, protocolId) {
            this.popoverVisible = false;
            const _this = this;
            this.tableVm.loading = true;
            _HTTP.get("/api/protocol/getProtocolOutXml?protocolId=" + protocolId, null, {
                success(res) {
                    _this.tableVm.loading = false;
                    // console.log(res.content)
                    const blob = new Blob([res.content], {
                        type:
                            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
                    });
                    if (window.navigator.msSaveBlob) {
                        //没有此判断的话，ie11下的导出没有效果
                        window.navigator.msSaveBlob(blob, unescape(protocolName + ".xml"));
                    } else {
                        const downloadElement = document.createElement("a");
                        const href = window.URL.createObjectURL(blob); //创建下载的链接
                        downloadElement.href = href;
                        downloadElement.download = unescape(protocolName + ".xml");
                        document.body.appendChild(downloadElement);
                        downloadElement.click(); //点击下载
                        document.body.removeChild(downloadElement); //下载完成移除元素
                        window.URL.revokeObjectURL(href); //释放掉blob对象
                    }
                },
                fail(res) {
                    _this.tableVm.loading = false;
                    console.log(res)
                }
            })
        },
        chooseOut(chooseIndex) {
            this.popoverVisible = true;
            this.currentIndex = chooseIndex
        },
        /**
         * 获取网关列表
         */
        getNets() {
            let that = this
            this.selectorLoading = true
            _HTTP.get("/api/protocol/nets", "", {
                success: function (res) {
                    // console.log(res)
                    that.nets = res
                    that.selectorLoading = false
                },
                fail: function (res) {
                    console.log("Error!", res)
                    that.selectorLoading = false
                }
            })
        },
        /**
         * 跳转配置页面
         * @param stype 协议类型
         * @param id 识别id
         */
        goConfig: function (stype, id, protocolName) {
            // console.log(stype, id)
            this.mrouter.pagePath = top._CTX + '/web/protocolc/config?id=' + id + '&stype=' + stype + '&protocolName=' + protocolName;
            let iframe = parent.window.document.getElementById("menuFrame")
            // console.log(iframe)
            iframe.setAttribute("src", this.mrouter.pagePath)
        },
        /**
         * 跳转traplist页面
         * @param mib_id
         */
        goTrapList: function (mib_id, protocolName) {
            this.mrouter.pagePath = top._CTX + '/web/protocolc/trap?id=' + mib_id + "&protocolName=" + protocolName;
            let iframe = parent.window.document.getElementById("menuFrame");
            // console.log(iframe)
            iframe.setAttribute("src", this.mrouter.pagePath);
        },
        reloadIframe: function () {
            lframe = document.getElementById('menuFrame')
            console.log(lframe)
        },
        importSuccess: function (res, file, fileList) {
            // console.log('res',res.data.content)
            // console.log('file',file)
            // console.log("=========================================")
            this.requsetPage()
            this.clearFiles()
        },
        handleRemove(file, fileList) {
            // console.log(file, fileList);
        },
        handlePreview(file) {
            // console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件`);
            this.clearFiles()
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${file.name}？`);
        },
        /**
         * 下拉框点击监听
         */
        selectorPulled() {
            this.getNets()
        }
    }
});
var app = new Vue(config)